# Maintainer: Ray Donnelly <mingw.android@gmail.com>

_realname=cvxopt
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=1.2.7
pkgrel=1
pkgdesc="Convex optimization based on the Python programming language (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url='https://cvxopt.org/'
license=('GPL v3')
validpgpkeys=('gpg_KEY')
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools" "${MINGW_PACKAGE_PREFIX}-cc")
depends=("${MINGW_PACKAGE_PREFIX}-python"
         $([[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]] &&
           echo "${MINGW_PACKAGE_PREFIX}-openblas" ||
           echo "${MINGW_PACKAGE_PREFIX}-f2cblaslapack")
         "${MINGW_PACKAGE_PREFIX}-suitesparse"
         "${MINGW_PACKAGE_PREFIX}-gsl"
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-dsdp"
         "${MINGW_PACKAGE_PREFIX}-glpk")
options=('staticlibs' 'strip' '!debug')
source=("https://github.com/cvxopt/cvxopt/archive/${pkgver}.tar.gz")
sha256sums=('26557c00b07aed6e6d7e7110443472766f3664faa43733c8cc520e4d9b856594')

prepare() {
  cd "${srcdir}"
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}"
}

build() {
  cd "${srcdir}/python-build-${MSYSTEM}"

  CVXOPT_BLAS_LIB=$([[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]] && echo 'openblas'  || echo 'blas') \
  CVXOPT_LAPACK_LIB=$([[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]] && echo 'openblas'  || echo 'lapack') \
  CVXOPT_BUILD_GSL=1                                  \
  CVXOPT_BUILD_FFTW=1                                 \
  CVXOPT_BUILD_DSDP=1                                 \
  CVXOPT_BUILD_GLPK=1                                 \
  CVXOPT_GSL_LIB_DIR=${MINGW_PREFIX}/lib              \
  CVXOPT_GSL_INC_DIR=${MINGW_PREFIX}/include          \
  CVXOPT_FFTW_LIB_DIR=${MINGW_PREFIX}/lib             \
  CVXOPT_FFTW_INC_DIR=${MINGW_PREFIX}/include         \
  CVXOPT_GLPK_LIB_DIR=${MINGW_PREFIX}/lib             \
  CVXOPT_GLPK_INC_DIR=${MINGW_PREFIX}/include         \
  CVXOPT_DSDP_LIB_DIR=${MINGW_PREFIX}/lib             \
  CVXOPT_DSDP_INC_DIR=${MINGW_PREFIX}/include/dsdp    \
  CVXOPT_SUITESPARSE_LIB_DIR=${MINGW_PREFIX}/lib      \
  CVXOPT_SUITESPARSE_INC_DIR=${MINGW_PREFIX}/include  \
    ${MINGW_PREFIX}/bin/python3 setup.py build
}

check() {
  cd "${srcdir}/python-build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/python setup.py check
}

package() {
  cd "${srcdir}/python-build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/COPYING"
}
